cmake_minimum_required(VERSION 3.19)
project(mapper)

# 设置 C++ 标准
set(CMAKE_CXX_STANDARD 11)

# 添加 UTF-8 编译选项（可选）
add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")

# 包含头文件路径
include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}/src
    ${CMAKE_CURRENT_SOURCE_DIR}/src/aop
    ${CMAKE_CURRENT_SOURCE_DIR}/src/builder
    ${CMAKE_CURRENT_SOURCE_DIR}/src/common
    ${CMAKE_CURRENT_SOURCE_DIR}/src/entity
    ${CMAKE_CURRENT_SOURCE_DIR}/src/guard
    ${CMAKE_CURRENT_SOURCE_DIR}/src/sql
    ${CMAKE_CURRENT_SOURCE_DIR}/src/util
    /usr/include/mysql
)

link_directories(/usr/lib64/mysql)

# 递归查找所有的源文件
file(GLOB_RECURSE MAPPER_SOURCES
    ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp
)

# 创建动态库
add_library(mapper SHARED ${MAPPER_SOURCES})

# 将头文件路径暴露给依赖的项目
target_include_directories(mapper PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}/src
    ${CMAKE_CURRENT_SOURCE_DIR}/src/aop
    ${CMAKE_CURRENT_SOURCE_DIR}/src/builder
    ${CMAKE_CURRENT_SOURCE_DIR}/src/common
    ${CMAKE_CURRENT_SOURCE_DIR}/src/entity
    ${CMAKE_CURRENT_SOURCE_DIR}/src/guard
    ${CMAKE_CURRENT_SOURCE_DIR}/src/sql
    ${CMAKE_CURRENT_SOURCE_DIR}/src/util
    /usr/include/mysql
)

# 链接 pthread 库和 MySQL 库
target_link_libraries(mapper PRIVATE pthread libmysqlclient.so)

# 设置安装路径为当前项目的 lib 目录
set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/lib)

# 安装动态库到 lib 目录
install(TARGETS mapper DESTINATION .)

# 安装头文件到 lib/include 目录，保持目录结构
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src/
        DESTINATION include/mapper
        FILES_MATCHING PATTERN "*.hpp" PATTERN "*.h")
